package cn.edu.jxust.controller.error;

import cn.edu.jxust.common.domain.web.error.IAirQualityErrorController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

/**
 * @author QiuKangming
 * @version 1.0
 * @description 错误处理控制器
 * @since 2020/06/30 19:10
 */
@RestController
public class AirQualityErrorController implements IAirQualityErrorController {

    @Override
    public Result handleError(HttpServletRequest request){

        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        ResultStatus resultStatus;
        if(statusCode == 404){
            resultStatus = ResultStatus.NOT_FOUND;
        }else if(statusCode == 400){
            resultStatus = ResultStatus.FAILED;
        }else if(statusCode == 405){
            resultStatus = ResultStatus.METHOD_NOT_SUPPERED;
        }else if(statusCode == 500){
            resultStatus = ResultStatus.SYSTEM_ERROR;
        }else {
            resultStatus = ResultStatus.valueOf("未知错误");
        }
        return ResultUtils.resultWithoutData(resultStatus);

    }

    /**
     * 返回错误路径
     * @return String
     */
    @Override
    public String getErrorPath() {
        return "/error";
    }

}
